package com.tensquare.user.service;

import cn.hutool.crypto.SecureUtil;
import com.tensquare.entity.IdWorker;
import com.tensquare.user.dao.UserDao;
import com.tensquare.user.pojo.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by WF on 2020/12/21 16:24
 */
@Service
@Transactional
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private IdWorker idWorker;
    //1.通过rabbitmq发送相关的验证码消息
    public void sendMessage(String mobile) {
        //1.1)生成六位数的验证码
        int min = 100000;   //六位数的最小值
        int max = 999999;   //六位数的最大值
        Random random = new Random();
        int code = random.nextInt(max-min);  //得到一个[0,max-min)范围的随机数
        if(code < min){
            code = code + min ;
        }
        System.out.println("生成的六位数的验证码是：" + code);

        //1.2)将验证码放到redis中
        redisTemplate.opsForValue().set(mobile,code + "",10, TimeUnit.HOURS);

        //1.3)发送消息
        //1.3.1)定义消息的内容
        Map map = new HashMap<>();
        //1.3.2)向map中添加内容
        map.put("mobile",mobile);       //手机号
        map.put("code",code+"");        //验证码
        //1.3.3)开始发送消息
        rabbitTemplate.convertAndSend("sms",map);
    }

    //2.用户注册
    public boolean add(User user,String code) {
        //2.1)从redis中得到验证码
        String validCode = (String) redisTemplate.opsForValue().get(user.getMobile());
        //2.2)与传入的用户输入的验证码比较
        if(!code.equals(validCode)){
            return false;
        }
        user.setId( idWorker.nextId()+"" );
        user.setFollowcount(0);//关注数
        user.setFanscount(0);//粉丝数
        user.setOnline(0L);//在线时长
        user.setRegdate(new Date());//注册日期
        user.setUpdatedate(new Date());//更新日期
        user.setLastdate(new Date());//最后登陆日期
        //user.setPassword(SecureUtil.md5(user.getPassword()));  //密码用md5加密
        String pwd = passwordEncoder.encode(user.getPassword()); //将密码进行BCryptPasswordEncoder加密
        user.setPassword(pwd);

        //添加用户
        userDao.save(user);
        return true;
    }

    //3.用户登录
    public User findUser(User user) {
        //3.1)根据手机号查询用户
        User loginUser = userDao.findByMobile(user.getMobile());
        //3.2)判断是否存在，如果存在，就验证密码
        if(loginUser != null && passwordEncoder.matches(user.getPassword(),loginUser.getPassword())){
            return loginUser;
        }
        return null;
    }

    //4.删除用户
    public void deleteById(String userId) {
        userDao.deleteById(userId);
    }

    //5.修改关注数
    public void incFollow(String userid, int x) {
        userDao.incFollow(userid,x);
    }

    //6.修改粉丝数
    public void incfans(String userid, int x) {
        userDao.incFans(userid,x);
    }
}
